/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBank;

import com.SySammy.SdBank.Prop.BankProperties;
import com.nijiko.coelho.iConomy.iConomy;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.PersistenceException;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class sdB extends JavaPlugin {

    public static void main(String[] args) {
        // TODO Debug
    }
    private static final Logger log = Logger.getLogger("Minecraft");
    private static final String currencyDIR = "plugins/RPGmod/Bank/";
    public PermissionHandler Permissions;
    private boolean UsePermissions;
    private static PluginListener PluginListener = null;
    private static iConomy iConomy = null;
    private static Server Server = null;

    @Override
    public void onLoad() {
        new File(currencyDIR).mkdirs();
    }

    @Override
    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    @Override
    public void onEnable() {

        //Setups
        BankProperties.loadCurrencyProps(false, 0);
        setupPermissions();
        getCommand("bank").setExecutor(new sdCBankCmds(this));
        setupDatabase();
        ContinueLoans();
        //logger
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");

        //iConomy stuff
        Server = getServer();
        PluginListener = new PluginListener();
        getServer().getPluginManager().registerEvent(Event.Type.PLUGIN_ENABLE, PluginListener, Priority.Monitor, this);
    }

    private void ContinueLoans() {
        BankManager BH = new BankManager(this);
        if (getBank().getLoans() != null) {
            Iterator<Bank> loans = getBank().getLoans().iterator();
            while (loans.hasNext()) {
                Bank pLoan = loans.next();
                String p = pLoan.getPlayerName();
                long time = pLoan.getTimeLeft();
                int Schedule = this.getServer().getScheduler().scheduleSyncDelayedTask(this, new LoanRun(p, this), (time * 86400 * 20L));
                int ScheduleRep = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new RepeatingTask(p, this), BankProperties.ScheduleRepTime * 3600 * 21L, BankProperties.ScheduleRepTime * 3600 * 20L);
                getBank().setSchedulers(p, Schedule, ScheduleRep);
            }
        }
    }

    private void setupDatabase() {
        try {
            getDatabase().find(Bank.class).findRowCount();
        } catch (PersistenceException ex) {
            System.out.println("Installing database for " + getDescription().getName() + " due to first time usage");
            installDDL();
        }
    }

    @Override
    public List<Class<?>> getDatabaseClasses() {
        List<Class<?>> list = new ArrayList<Class<?>>();
        list.add(Bank.class);
        return list;
    }

    public BankManager getBank() {
        return new BankManager(this);
    }

    public static Server getBukkitServer() {
        return Server;
    }

    public static iConomy getiConomy() {
        return iConomy;
    }

    public static boolean setiConomy(iConomy plugin) {
        if (iConomy == null) {
            iConomy = plugin;
        } else {
            return false;
        }
        return true;
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdCurrency.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    public boolean casUseBankInfo(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.info");
        }
        return p.isOp();
    }

    public boolean casUseBankLoan(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.loan");
        }
        return p.isOp();
    }

    public boolean casUseBankRepay(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.repay");
        }
        return p.isOp();
    }

    public boolean casUseBankHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.help");
        }
        return p.isOp();
    }
}
